<?php
/**
 * 消息类
 */
class Notice extends MyBase{

	const NEW_ORDER	= '101';	// 新订单
	const PAYING	= '102';	// 新付款
	const PAID		= '103';	// 新已付款
	const RECEIVE	= '104';	// 新收货
	const COMMENTED	= '105';	// 新评价
	const LACK		= '201';	// 缺货
	const FAVORITE	= '202';	// 收藏

	function __construct(){
		parent::__construct('notice.id');
	}

	/**
	 * 添加消息
	 * @param int $type 消息类型
	 * @param array $target 消息主体对象，比如新订单，这里是订单的编号
	 * @param string $msg  消息内容
	 */
	public function add($type, $target, $msg = ''){
		$_f_notice = cache_read('notices.php');
		if(empty($_f_notice)){
			$_f_notice = array();
		}elseif(count($_f_notice) > 100){
			array_shift($_f_notice);	// 仅保留最新折100条消息
		}

		$notice = array(
			'type'		=> $type,
			'msg'		=> $msg,
			'target'	=> $target,
			'time'		=> TIME,
		);
		$notice['id'] = $this->db->insert($this->tbname, $notice);	// 保存到数据表
		$_f_notice[] = $notice;
		return cache_write('notices.php', $_f_notice);
	}

	public function get($key = ''){
		$_f_notice = cache_read('notices.php');
		if(empty($_f_notice)){
			return false;
		}else{
			$notice = array_shift($_f_notice);
			cache_write('notices.php', $_f_notice);
			$this->update($notice['id'], array('read' => 1));
			// $notice['type'] = Statics::notice_type_arr[$notice['type']];
			return $notice;
		}
	}
}
?>